package com.example.module.system.config;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.resource.ResourceStore;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import cloud.tianai.captcha.resource.common.model.dto.ResourceMap;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static cloud.tianai.captcha.common.constant.CommonConstant.DEFAULT_TAG;
import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.TEMPLATE_ACTIVE_IMAGE_NAME;
import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.TEMPLATE_FIXED_IMAGE_NAME;

@Configuration
@RequiredArgsConstructor
public class CaptchaResourceConfiguration {

    private final ResourceStore resourceStore;

    public static final String DEFAULT_TYPE = "classpath";

    public static final String RESOURCE_PATH = "bgimages";

    public static final String TEMPLATE_PATH = "template";

    @PostConstruct
    public void init() throws IOException {
        // 添加背景图片资源
        ClassPathResource classPathResource = new ClassPathResource(RESOURCE_PATH);
        Path path = Paths.get(classPathResource.getURI());
        try (Stream<Path> paths = Files.walk(path, 1)) {
            paths.filter(Files::isRegularFile)
                    .filter(p -> p.toString().endsWith(".jpg") || p.toString().endsWith(".png"))
                    .forEach(p -> {
                        String fileName = p.getFileName().toString();
                        String filePath = RESOURCE_PATH + "/" + fileName;

                        resourceStore.addResource(CaptchaTypeConstant.ROTATE, new Resource(DEFAULT_TYPE, filePath, DEFAULT_TAG));
                        resourceStore.addResource(CaptchaTypeConstant.CONCAT, new Resource(DEFAULT_TYPE, filePath, DEFAULT_TAG));
                        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(DEFAULT_TYPE, filePath, DEFAULT_TAG));
                        resourceStore.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource(DEFAULT_TYPE, filePath, DEFAULT_TAG));
                    });
        }

        // 添加模板资源
        // 滑动验证
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, new ResourceMap(DEFAULT_TAG, 4) {{
            put(TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/slider_1/active.png")));
            put(TEMPLATE_FIXED_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/slider_1/fixed.png")));
        }});
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, new ResourceMap(DEFAULT_TAG, 4) {{
            put(TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/slider_2/active.png")));
            put(TEMPLATE_FIXED_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/slider_2/fixed.png")));
        }});
        // 旋转验证
        resourceStore.addTemplate(CaptchaTypeConstant.ROTATE, new ResourceMap(DEFAULT_TAG, 4) {{
            put(TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/rotate_1/active.png")));
            put(TEMPLATE_FIXED_IMAGE_NAME, new Resource(DEFAULT_TYPE, TEMPLATE_PATH.concat("/rotate_1/fixed.png")));
        }});
    }
}
